//
//  HJStatusViewCell.swift
//  Micro-blog Sina
//
//  Created by admin on 16/3/25.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SnapKit

class HJStatusViewCell: UITableViewCell {
    // MARK: - 属性
    var bottomViewBottomCons: Constraint?
    
    // Status model
    var status: HJStatuses? {
        didSet {
            originalView.status = status
            
            // Delete original constraint
            bottomViewBottomCons?.uninstall()
            if let retweetStatus = status?.retweeted_status {
                retweetView.hidden = false
                retweetView.retweetStatus = retweetStatus
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else {
                retweetView.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI()
    }
    
    private func prepareUI() {
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            self.bottomViewBottomCons = make.top.equalTo(originalView.snp_bottom).constraint
            make.height.equalTo(44)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            // contentView底部参照originalView
            make.bottom.equalTo(bottomView)
        }
    }
    
    // MARK: - 懒加载
    private lazy var originalView: HJStatusOriginalView = HJStatusOriginalView()
    private lazy var bottomView: HJStatusBottomView = HJStatusBottomView()
    private lazy var retweetView: HJStatusRetweetView = HJStatusRetweetView()
}
